package photobox

import grails.converters.JSON
import photobox.account.PublicizeImg
import photobox.account.User
import photobox.order.Order
import photobox.photo.Category
import photobox.photo.Photo
import photobox.photo.PhotoComment

class HomeController {

    def index() {

        def hql_t = "from Category as C where C.categoryType = 1"
        def categories_theme = Category.findAll(hql_t)
        def hql_s = "from Category as C where C.categoryType = 2"
        def categories_style = Category.findAll(hql_s)
        //图片创建时间降序，推荐图片
        def hql_1 = "from Photo as P order by P.dateCreated desc"
        def    photoListNew = Photo.findAll(hql_1, [max: 6])
        //图片创建时间降序，推荐图片
        def hql_2 = "from Photo as P where P.isRecommend = 1 order by P.dateCreated desc"
        def photoListRecommend = Photo.findAll(hql_2, [max: 6])
        def publicizeImg = PublicizeImg.findAllByIsShow(1)
        //def logImage = PublicizeImg.findByLocation("LOG")
        //本月销售前三图片（如何限制时间在本月条件未加）待完善
        //def hql_3 = "from Photo as P order by P.saleNum asc"
        def calendar  =   new  GregorianCalendar()
        calendar.set(GregorianCalendar.DAY_OF_MONTH, 1);
        def beginTime = calendar.getTime();
        def condition = new HashMap()
        condition['dateCreated'] = beginTime
        def hql_3 = " from Order as O join O.items as OI WHERE O.dateCreated >= :dateCreated GROUP BY OI.photo.id ORDER BY SUM(OI.number) DESC "
        def saleWellPhoto = Order.executeQuery("select OI.photo " +hql_3,condition,[max:3])
        //累计销售图前三图片
        def hql_4 ="from Photo as P order by P.saleNum desc"
        def totalSaleWell = Photo.findAll(hql_4,[max:3])
        //本月最佳摄影师
        def hql_5 =  " from Order as O join O.items as OI WHERE O.dateCreated >= :dateCreated GROUP BY OI.photo.id ORDER BY SUM(OI.number) DESC "
        def users = Order.executeQuery("select OI.photo.user" +hql_5,condition, [max:3])
        def photographer = []
        users.each {
            def photoNum = Photo.countByUser(it)
            photographer.push(
                    id:it.id,
                    photoImage:it.photoImage,
                    nickName:it.nickName,
                    num:photoNum
            )
        }
        def hql_6 = " select P.user from Photo as P GROUP BY P.user order by SUM(P.saleNum) desc"
        def totalUsers =Photo.executeQuery(hql_6, [], [max:3])
        def totalPhotographer = []
        totalUsers.each {
            def photoNum = Photo.countByUser(it)
            totalPhotographer.push(
                    id:it.id,
                    photoImage:it.photoImage,
                    nickName:it.nickName,
                    num:photoNum
            )
        }
        [categories_theme: categories_theme,categories_style:categories_style, photoListNew: photoListNew, photoListRecommend: photoListRecommend, users: photographer,totalUsers:totalPhotographer, publicizeImg: publicizeImg ,photoList:saleWellPhoto, TotalSaleWell:totalSaleWell]
    }
    def photographer(){
        def max = params.max?:9
        def offset = params.offset?:0
        def hql = "from User as U where isPhotographer=1 "
        def users = User.executeQuery(hql, [max: max, offset: offset])
        def total = Photo.executeQuery("select count(*)"+hql)
        [users: users, total: total]
    }
    def comment(){
        def max = params.max?:9
        def offset = params.offset?:0
        def hql = "from PhotoComment as C "
        def comments = PhotoComment.executeQuery(hql, [max: max, offset: offset])
        def total = Photo.executeQuery("select count(*)"+hql)
        [comments: comments, total: total]
    }
    def bestPhotographer(){
        def user = User.get(params.userId)
        def hql = "from Photo as P where P.user ="+params.userId+""
        def photoList = Photo.executeQuery(hql)
        def num = photoList.size();
        if(params.json){
            def max = params.max
            def offset = params.offset
            //def photoList = Photo.findAllByKeywordsLike(keyword, [max:max, offset:offset])
            photoList = Photo.executeQuery(hql, [], [max:max, offset:offset])
            def photoLists = []
            photoList.each{
                photoLists.push(
                        id: it.id,
                        smallImagePath: it.smallImagePath,
                        name: it.name,
                        price: it.price,
                        username: it.user?.username
                )
            }
            def o = max.toInteger() +  offset.toInteger()
            render( [photoList:photoLists, offset: o,max:max,userId:params.id] as JSON)
        }
        [userId:params.userId,user:user,num:num]
    }
}
